//========================================================================== 
// SuperMap AjaxClient 客户端程序，版权所有，北京超图软件股份有限公司，2000-2009。 
// 本程序只能在有效的授权许可下使用。未经许可，不得以任何手段擅自使用或传播。 
// 作者：			SuperMap WebClient Team 
// 修改：	 
// 文件名：			SuperMap.Web.Mapping.TiledIServerLayer.js  
// 功能：			 googleMapsLayer
// 最后修改时间：	2009-8-26
//========================================================================== 
Type.registerNamespace('SuperMap.Web.Mapping');

SuperMap.Web.Mapping.TiledGMapsLayer = function() {
		///<param name="url" type="String"></param>

	SuperMap.Web.Mapping.TiledGMapsLayer.initializeBase(this);
	
	this._subDomains = [0, 1, 2, 3];
	
	this._mapTileUri = "http://mt{0}.google.com/mt/v=ap.92&hl=zh-CN&z={1}&x={2}&y={3}&s=";
	
	this._satelliteTileUri = "http://khm{0}.google.com/maptilecompress/t=3&q=25&hl=zh-CN&x={2}&y={3}&z={1}&s=Gal";
	
	this._cornerCoordinate = 20037508.3427892;
	
	this._wkid = 102113;
	
	this._mapType = "map";
};

SuperMap.Web.Mapping.TiledGMapsLayer.prototype = {
//===============TiledGMapsLayer properties start========================
	get_mapType:function() {
		return this._mapType;
	},
	set_mapType:function(mapType) {
		this._mapType = mapType;
	},
	
//===================TiledGMapsLayer properties end========================

//=================TiledGMapsLayer public methods start========================
	/**
	*返回iServer地图瓦片的url。
	*/
	_getTileUrl:function(x, y, resolution) {
		var e = Function._validateParams(arguments, [{name: "x", type: Number}, {name: "y", type: Number}, {name: "resolution", type: Number}]);
		if (e) throw e;
		var url = null;
		
		var level = this._map.get_level();
		
		//当超出图片范围返回为null
		if(x < 0 || y < 0 || x > Math.pow(2, level) - 1 || y > Math.pow(2, level) - 1) {
			url = "images/blank_256.png";
			return url;
		}
		
		//随机选择服务器
		var subDomain = this._subDomains[( x + y + level) % this._subDomains.length];
		
		
		//判断map类型
		switch(this._mapType) {
			case "map":
			//http://mt0.google.cn/vt/v=w2.106&hl=zh-CN&gl=cn&x=842&y=387&z=10&s=G
				url = "http://mt" + subDomain + ".google.cn/vt/v=w2.106&hl=zh-CN&gl=cn&z=" + level + "&x=" + x + "&y=" + y + "&s=G";
				break;
			case "satellite":
				url = "http://khm" + subDomain + ".google.com/kh/v=45&x=" + x + "&y=" + y + "&z=" + level + "&s=Gal";
				break;
			case "communication":
				url = "http://mt" + subDomain + ".google.cn/vt/v=w2t.106&hl=zh-CN&gl=cn&x=" + x + "&y=" + y + "&z=" + level + "&s=Galile"
				break;
		}
		//if(this._mapType == "map") {
			
		//}
		//else if(this._mapType == "satellite") {
			//todo;
			//var url = "http://khm" + subDomain + ".google.com/maptilecompress/t=3&q=25&hl=zh-CN&x=" + x + "&y=" + y + "&z=" + level + "&s=Gal";
			//http://mt0.google.cn/vt/v=w2t.106&hl=zh-CN&gl=cn&x=0&y=0&z=1&s=
			//var url = "http://maps.gstatic.cn/intl/zh-CN_cn/mapfiles/transparent.png";
			//http://khm3.google.cn/kh/v=45&x=55&y=22&z=6&s=Gal
			//var url = "http://khm" + subDomain + ".google.com/kh/v=45&x=" + x + "&y=" + y + "&z=" + level + "&s=Gal";
			//console.log(url);
		//}
		
		return url;
	},
//================TiledGMapsLayer public methods end========================

//================TiledGMapsLayer private methods started========================
	
	initialize:function() {
	/// <summary>初始化方法</summary>
	///<returns type="void"></returns>
		this._bounds = new SuperMap.Web.Core.Rectangle2D(-this._cornerCoordinate, -this._cornerCoordinate, this._cornerCoordinate, this._cornerCoordinate);
		this._tileSize = 256;
		
		//计算resolutions数组
		var res = this._cornerCoordinate * 2 / 256;
		var resolutions = new Array();
        for (var i = 0; i < 22; i++)
        {
            resolutions[i] = res;
            res *= 0.5;
        }
        this._resolutions = resolutions;
		
		this._url = this._mapTileUri;
		
		this._viewBoundsChangedDelegate = Function.createDelegate(this, this._viewBoundsChanged);
		this._unLoadDelegate = Function.createDelegate(this, this._unLoad);
		this._crs = new SuperMap.Web.Core.CoordinateReferenceSystem(3857, "undefined");
		
		
		this._initialized = true;
		this.raise_initialized(this);
	},
	
	/**
	 * 设置Map
	 * @param {SuperMap.Web.Mapping.Map} map
	 */
	setMap:function(map) {
//		var e = Function._validateParams(arguments, [{name: "map", type: SuperMap.Web.Mapping.Map}]);
//		if (e) throw e;
		
		this._map = map;
		
		this._viewBounds = this._map.get_viewBounds().clone();
		this._map.add_viewBoundsChanged(this._viewBoundsChangedDelegate);
		this._map.add_layerRemoved(this._unLoadDelegate);
	},
//=============TiledGMapsLayer private methods end=========================

//===============TiledGMapsLayer events start========================
	/**
	*刷新图层时触发。
	*/
	add_refreshing:function(handler) {
		this._addEvent("refreshing", handler);
	},
	remove_refreshing:function(handler) {
		this._removeEvent("refreshing", handler);
	},
	raise_refreshing:function(arguments, userContext) {
		this._raiseEvent("refreshing", arguments, userContext);
	},
	
	_addEvent:function(eventName, handler) {
		this.get_events().addHandler(eventName, handler);
	},
	_removeEvent:function(eventName, handler) {
		this.get_events().removeHandler(eventName, handler);
	},
	_raiseEvent:function(eventName, arguments, userContext) {
		var handler = this.get_events().getHandler(eventName);
		if(handler) {
			handler(arguments, userContext);
		}
	}
//===============TiledGMapsLayer events end========================
};
SuperMap.Web.Mapping.TiledGMapsLayer.registerClass('SuperMap.Web.Mapping.TiledGMapsLayer', SuperMap.Web.Mapping.TiledCachedLayer);